package designModel;
// 职责链（Chain of Responsibility）模式：把请求从链中的一个对象传到下一个对象，直到请求被响应为止。通过这种方式去除对象之间的耦合。 ☆☆☆☆☆
public class Chain {
    public static void main(String[] args) {
        RHandler manager = new MHandler();
        RHandler departmentHead = new DHandler();
        RHandler finance = new FHandler();

        manager.setSuccessor(departmentHead);// 将下级的处理对象给设置进去
        departmentHead.setSuccessor(finance);

        RRequest request1 = new RRequest(800, "购买办公用品");
        RRequest request2 = new RRequest(3000, "参加培训");
        RRequest request3 = new RRequest(10000, "举办团建活动");

        manager.handleRequest(request1); // 会依次判断能否处理
        manager.handleRequest(request2); // 会依次判断能否处理
        manager.handleRequest(request3); // 会依次判断能否处理
    }
}
class RRequest {
    private double amount;
    private String description;

    public RRequest(double amount, String description) {
        this.amount = amount;
        this.description = description;
    }

    public double getAmount() {
        return amount;
    }

    public String getDescription() {
        return description;
    }
}
// 然后，创建一个抽象处理者类 RHandler
abstract class RHandler {
    protected RHandler successor; // 下一个处理者

    public void setSuccessor(RHandler successor) {
        this.successor = successor;
    }

    public abstract void handleRequest(RRequest request);
}

// 接下来，实现具体的处理者类：经理、部门主管和财务部门处理者。
class MHandler extends RHandler {
    @Override
    public void handleRequest(RRequest request) {
        if (request.getAmount() <= 1000) {
            System.out.println("经理处理报销请求：" + request.getDescription());
        } else if (successor != null) {
            successor.handleRequest(request);
        }
    }
}

class DHandler extends RHandler {
    @Override
    public void handleRequest(RRequest request) {
        if (request.getAmount() <= 5000) {
            System.out.println("部门主管处理报销请求：" + request.getDescription());
        } else if (successor != null) {
            successor.handleRequest(request);
        }
    }
}

class FHandler extends RHandler {
    @Override
    public void handleRequest(RRequest request) {
        System.out.println("财务部门处理报销请求：" + request.getDescription());
    }
}
